package school.valon.business.impl;

import org.springframework.transaction.annotation.Transactional;

import school.valon.UserContext;
import school.valon.business.SurveyBusiness;
import school.valon.business.base.BaseBusiness;
import school.valon.dto.SurveyAnswerDto;
import school.valon.dto.SurveyQuestionDto;
import school.valon.persistence.dao.StaffDao;
import school.valon.persistence.dao.SurveyAnswerDao;
import school.valon.persistence.dao.SurveyQuestionDao;
import school.valon.persistence.dao.UserDao;
import school.valon.persistence.pojo.SurveyAnswer;
import school.valon.persistence.pojo.SurveyQuestion;

@Transactional
public class SurveyBusinessImpl extends BaseBusiness implements SurveyBusiness {

	private UserDao userDao;
	private StaffDao staffDao;
	private SurveyQuestionDao surveyQuestionDao;
	private SurveyAnswerDao surveyAnswerDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public void setStaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
	}
	public void setSurveyQuestionDao(SurveyQuestionDao surveyQuestionDao) {
		this.surveyQuestionDao = surveyQuestionDao;
	}
	public void setSurveyAnswerDao(SurveyAnswerDao surveyAnswerDao) {
		this.surveyAnswerDao = surveyAnswerDao;
	}

	public SurveyQuestionDto saveSurveyQuestion(SurveyQuestionDto surveyQuestionDto) {
		SurveyQuestion surveyQuestion = null;
		if (surveyQuestionDto.getSurveyQuestionId() == null) {
			surveyQuestion = mapper.map(surveyQuestionDto, SurveyQuestion.class);
			if (surveyQuestionDto.getStaff() != null && surveyQuestionDto.getStaff().getStaffId() != null) {
				surveyQuestion.setStaff(staffDao.find(surveyQuestionDto.getStaff().getStaffId()));
			}
			surveyQuestion = surveyQuestionDao.create(surveyQuestion);
		} else {
			surveyQuestion = surveyQuestionDao.find(surveyQuestionDto.getSurveyQuestionId());
			surveyQuestion.setCreatedOn(surveyQuestionDto.getCreatedOn());
			surveyQuestion.setEndDate(surveyQuestionDto.getEndDate());
			surveyQuestion.setStartDate(surveyQuestionDto.getStartDate());
			surveyQuestion.setSurveyQuestionName(surveyQuestionDto.getSurveyQuestionName());
			surveyQuestion.setSurveyQuestionText(surveyQuestionDto.getSurveyQuestionText());
			surveyQuestionDao.update(surveyQuestion);
		}

		SurveyQuestionDto newSurveyQuestionDto = mapper.map(surveyQuestion, SurveyQuestionDto.class);
		newSurveyQuestionDto.setStaff(null);
		newSurveyQuestionDto.setSurveyAnswers(null);
		return newSurveyQuestionDto;
	}

	public void deleteSurveyQuestion(Integer surveyQuestionId) {
		surveyQuestionDao.delete(surveyQuestionId);
	}

	public SurveyAnswerDto saveSurveyAnswer(SurveyAnswerDto surveyAnswerDto) {
		SurveyAnswer surveyAnswer = null;
		if (surveyAnswerDto.getSurveyAnswerId() == null) {
			surveyAnswer = mapper.map(surveyAnswerDto, SurveyAnswer.class);
			surveyAnswer.setSurveyQuestion(surveyQuestionDao.find(
					surveyAnswerDto.getSurveyQuestion().getSurveyQuestionId()));
			surveyAnswer.setUser(userDao.find(UserContext.getCurrentUser().getUserId()));
			surveyAnswer = surveyAnswerDao.create(surveyAnswer);
		} else {
			surveyAnswer = surveyAnswerDao.find(surveyAnswerDto.getSurveyAnswerId());
			surveyAnswer.setSurveyAnswer(surveyAnswerDto.getSurveyAnswer());
			surveyAnswer.setSurveyAnswerDate(surveyAnswerDto.getSurveyAnswerDate());
			surveyAnswerDao.update(surveyAnswer);
		}
		SurveyAnswerDto newSurveyAnswerDto = mapper.map(surveyAnswer, SurveyAnswerDto.class);
		newSurveyAnswerDto.getSurveyQuestion().setStaff(null);
		newSurveyAnswerDto.getSurveyQuestion().setSurveyAnswers(null);
		newSurveyAnswerDto.setUser(null);
		return newSurveyAnswerDto;
	}

	public void deleteSurveyAnswer(Integer surveyAnswerId) {
		surveyAnswerDao.delete(surveyAnswerId);
	}
}
